
# 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
from rest_framework.views import exception_handler
# from AKapi.utils import response
from .response import APIResponse
from .logger import log


def common_exception_handler(exc, context):
    log.error('view:%s----error:%s' % (str(context['view'].__class__.__name__), str(exc)))
    """
    def exception_handler(exc, context):
    :param exc:
    :param context:
    :return:
    """
    print(context)
    print(context['view'])
    ret = exception_handler(exc=exc, context=context)  # ret 不为空的话是Response对象
    # 如果drf内部内置没有处理
    if not ret:
        # 处理drf没有处理的异常
        if isinstance(exc, KeyError):
            return APIResponse(code=0, msg='key error')
        return APIResponse(code=0, msg='error', result=str(exc))
    else:
        return APIResponse(code=0, result=ret.data, msg='error')